package com.scsj.supermarket.view.activity.personalsettingmodel;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.ItemTitlePagerAdapter;
import com.scsj.supermarket.customview.NoScrollViewPager;
import com.scsj.supermarket.customview.PagerSlidingTabStrip;
import com.scsj.supermarket.view.activity.baseactivitymodel.BaseActivity;
import com.scsj.supermarket.view.fragment.followmodel.FollowGoods;
import com.scsj.supermarket.view.fragment.followmodel.FollowShop;
import com.scsj.supermarket.view.fragment.followmodel.FollowShopFilter;

import java.util.ArrayList;
import java.util.List;


/**
 * 我的关注 商品关注、店铺关注
 */
public class MyFollowActivity extends BaseActivity implements View.OnClickListener {
    private Context mContext;
    private int type;
    //筛选
    private DrawerLayout drawer_layout;
    private FrameLayout drawer_content;
    private LinearLayout back_ll;//返回
    private LinearLayout goods_ll;//商品关注 所显示菜单
    private TextView goods_edit_tv;//编辑
    private Boolean isEdit = false;// 商品关注 是否为编辑状态
    public PagerSlidingTabStrip psts_tabs;
    public NoScrollViewPager vp_content;
    private List<Fragment> fragmentList = new ArrayList<>();
    private FollowGoods followGoods;
    private FollowShop followShop;

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_follow);
        mContext = this;
    }

    @Override
    protected void initView() {
        back_ll = findViewById(R.id.back_ll);

        //抽屉
        drawer_layout = findViewById(R.id.drawer_layout);
        drawer_content = findViewById(R.id.drawer_content);
        drawer_layout.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            }

            @Override
            public void onDrawerStateChanged(int newState) {
            }
        });

        goods_ll = findViewById(R.id.goods_ll);
        goods_edit_tv = findViewById(R.id.goods_edit_tv);
        psts_tabs = (PagerSlidingTabStrip) findViewById(R.id.psts_tabs);
        vp_content = (NoScrollViewPager) findViewById(R.id.vp_content);
        followGoods = new FollowGoods();
        followShop = new FollowShop();
        fragmentList.add(followGoods);
        fragmentList.add(followShop);
        vp_content.setAdapter(new ItemTitlePagerAdapter(getSupportFragmentManager(),
                fragmentList, new String[]{"商品", "店铺"}));
        vp_content.setOffscreenPageLimit(2);
        vp_content.setNoScroll(true);
        psts_tabs.setViewPager(vp_content);
        psts_tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    goods_ll.setVisibility(View.VISIBLE);
                } else {
                    goods_ll.setVisibility(View.GONE);
                }
//                Toasty.info(mContext,""+position).show();
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    @Override
    protected void initData() {
        type = getIntent().getExtras().getInt("type");
        vp_content.setCurrentItem(type);
    }

    @Override
    protected void initListener() {
        back_ll.setOnClickListener(this);
        goods_edit_tv.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_ll://返回
                finish();
                break;
            case R.id.goods_edit_tv://编辑
                isEdit = !isEdit;
                followGoods.setEdit(isEdit);
                if (isEdit) {
                    goods_edit_tv.setText("完成");
                } else {
                    goods_edit_tv.setText("编辑");
                }
                break;
            default:
                break;
        }
    }

    public void initFilter() {
        Fragment fragment = new FollowShopFilter();
        FragmentManager fragmentManager = getSupportFragmentManager();
        Bundle bundle = new Bundle();
        bundle.putString("departmentName", "");
        fragment.setArguments(bundle);
        fragmentManager.beginTransaction().replace(R.id.drawer_content, fragment).commit();
    }

    public void showFilter() {
        drawer_layout.openDrawer(drawer_content);
    }

}
